package com.cloudsubassemblykafka10024.serializer;

import com.cloudsubassemblykafka10024.serializer.User;
import com.cloudsubassemblykafka10024.serializer.UserDefineDeserializer;
import com.cloudsubassemblykafka10024.serializer.UserDefineSerializer;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Date;
import java.util.Properties;

/**
 * @Description: Kafka 生产者快速入门
 * @Author zx
 * @Date 2024/12/24 19:55
 * @Version 1.0
 */
public class KafkaProducerUser_001 {

    public static void main(String[] args) {

        // 创建 Kafka Producer
        Properties properties = new Properties();
        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.18.220:9092");
        properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, UserDefineSerializer.class.getName());

        KafkaProducer<String, User> kafkaProducer = new KafkaProducer<>(properties);

        // 发送消息
        for (int i = 0; i < 10; i++) {
            ProducerRecord<String, User> producerRecord = new ProducerRecord<>("messageDML002", "key" + 1,
                    new User("zx" + i, i, new Date()));
            kafkaProducer.send(producerRecord);
        }

        // 关闭资源
        kafkaProducer.close();

    }
}
